{% extends 'users/base.html' %}
{% block title_block %} 注册 {% endblock %}
{% block body_block %}
    {% csrf_token %}
    <div class="form-signin">
        <h2 class="form-signin-heading text-center">请注册</h2>
        <label for="inputText">用户名</label>
        <input type="text" id="inputUsername" class="form-control" placeholder="用户名" required autofocus>
        <label for="inputPassword">密码</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="密码" required>
        <label for="inputPasswordAgain">再次输入密码</label>
        <input type="password" id="inputPasswordAgain" class="form-control" placeholder="确认密码" required>
        <label>已经注册过？ <a href="{% url 'users:login' %}">点击登录</a></label>
        <button id="register_btn" class="btn btn-lg btn-primary btn-block" type="submit">注册</button>
    </div>
<script>
$(function(){
    $('#register_btn').on('click', function(){
        var data = {
            'username': $('#inputUsername').val(),
            'password': $('#inputPassword').val(),
            'passwordagain': $('#inputPasswordAgain').val(),
            'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val()
        }
        if(data['password'] != data['passwordagain']){
            $('#tip').html('两次密码输入不一致，请确认');
        }
        else{
            $.post('/oa/register/', data, function(res){
                if(res.status == 1){
                    $('#tip').html('注册成功，3秒后跳转首页!');
                    setTimeout(function(){
                        window.location.href='{% url "users:index" %}';
                    }, 3000)
                }else{
                    $('#tip').html(res.msg);
                }
            }, 'json')
        }
    }) 
})
</script>
{% endblock %}
